package org.edu.jgame.cardgames;

import java.io.*;
import java.net.*;
import java.util.Random;

public class clientThread extends Thread {

	DataInputStream is = null;
	PrintStream os = null;
	Socket clientSocket = null;
	clientThread t[];
	boolean is_endshuffle = false;
	boolean is_pass = false;
	int cur_state = 0;
	String pass = "";
	String name = "";

	public clientThread(Socket clientSocket, clientThread[] t) {
		this.clientSocket = clientSocket;
		this.t = t;
	}
	
	private boolean compareCard(int a, int b) {
		int a1 = a/10;
		int a2 = a%10;
		
		int b1 = b/10;
		int b2 = b%10;
		
		if(a2 > b2) {	// Compare card type 
			return true;
		}
		
		if((a2 == b2) && (a1 > b1)) {	// Compare card number
			return true;
		}
		
		return false;
	}
	
	
	private void BubbleSort(int[] arr) {
		boolean swapped = true;
		int j = 0;
		int tmp;
		while (swapped) {
			swapped = false;
			j++;
			for (int i = 0; i < arr.length - j; i++) {
				if (compareCard(arr[i], arr[i+1])) {
					tmp = arr[i];
					arr[i] = arr[i + 1];
					arr[i + 1] = tmp;
					swapped = true;
				}
			}
		}
	}
	
	private int[][] shuffle() {
		int[] cards = {21,22,23,24,31,32,33,34,41,42,43,44,51,
				52,53,54,61,62,63,64,71,72,73,74,81,82,83,84,91,
				92,93,94,101,102,103,104,111,112,113,114,121,122,
				123,124,131,132,133,134,141,142,143,144};
		
		// List of value card in each player
		int[][] p = new int[4][13];
		
		
		int n1, n2, n3, n4;
		n1 = n2 = n3 = n4 = 0;
		
		Random random = new Random();
		
		for(int i = 0; i < 52; i++) {
			int t = random.nextInt(4);		// Random to give the card to player
			
			while((t == 0 && n1 == 13) || (t == 1 && n2 == 13)
					|| (t == 2 && n3 == 13) || (t == 3 && n4 == 13)) {
				t = random.nextInt(4);		// ...
			}
			
			switch(t) {
			case 0:
				p[0][n1++] = cards[i];
				break;
			case 1:
				p[1][n2++] = cards[i];
				break;
			case 2:
				p[2][n3++] = cards[i];
				break;
			case 3:
				p[3][n4++] = cards[i];
				break;		
			}					
		}
		
		BubbleSort(p[0]);
		BubbleSort(p[1]);
		BubbleSort(p[2]);
		BubbleSort(p[3]);
		
		return p;
	}

	public void run() {
		String line = "";
		// String read = "";
		String name;
		try {
			is = new DataInputStream(clientSocket.getInputStream());
			os = new PrintStream(clientSocket.getOutputStream());
			
			//os.println("hello");
			line = is.readLine();
			if(line.startsWith("name")) {
				name = line.split(" ")[1];
				line = "";
			}
			
			if(t[0] != null && t[1] != null && t[2] != null && t[3] != null) {
				for(int i = 0; i < 4; i++) {
					if(t[i] != null) {
						t[i].os.println("shuffle");
					}
				}
			}
				
			while (true) {
				line = is.readLine();
				if (line.startsWith("quit"))
					break;
				if(line.equalsIgnoreCase("endshuffle")) {
					is_endshuffle = true;
				}
				if(t[0].is_endshuffle && t[1].is_endshuffle && t[2].is_endshuffle && t[3].is_endshuffle) {
					int[][] cards = shuffle();
					
					line = "<data>";
					String[] lines = new String[4];
					for(int i = 0; i < 4; i++) {
						lines[i] = "<p name='Player" + i + "' id='" + t[i].clientSocket.getPort() + "'>";
						for(int j = 0; j < 13; j++) {
							lines[i] += "<card value='" + cards[i][j] + "'/>";
						}
						lines[i] += "</p>";
					}
					
					t[0].os.println(line + lines[0] + lines[1] + lines[2] + lines[3] + "</data>");
					t[1].os.println(line + lines[1] + lines[2] + lines[3] + lines[0] + "</data>");
					t[2].os.println(line + lines[2] + lines[3] + lines[0] + lines[1] + "</data>");
					t[3].os.println(line + lines[3] + lines[0] + lines[1] + lines[2] + "</data>");		
					
					t[0].is_endshuffle = false;
					t[1].is_endshuffle = false;
					t[2].is_endshuffle = false;
					t[3].is_endshuffle = false;
				}				
				if(line.startsWith("<p ")) {
					pass = line;
				}
				if(line.equalsIgnoreCase("pass")) {
					is_pass = true;
				}
				if(t[0].is_pass && t[1].is_pass && t[2].is_pass && t[3].is_pass) {
					line = "<data>" + t[0].pass + t[1].pass + t[2].pass + t[3].pass + "</data>";
					for(int i = 0; i < 4; i++) {
						t[i].os.println(line);
					}
					t[0].is_pass = false;
					t[1].is_pass = false;
					t[2].is_pass = false;
					t[3].is_pass = false;
				}
				if(line.equalsIgnoreCase("play")) {
					cur_state = 1;
					pass = "";
				}
				if(cur_state == 1 && pass != "") {
					line = "<data>" + pass +"</data>";
					for(int i = 0; i < 4; i++) {
						t[i].os.println(line);
					}
					pass = "";
				}
			}

			// Clean up:
			// Set to null the current thread variable such that other client
			// could
			// be accepted by the server

			for (int i = 0; i < 4; i++)
				if (t[i] == this)
					t[i] = null;

			// close the output stream
			// close the input stream
			// close the socket

			is.close();
			os.close();
			clientSocket.close();
		} catch (IOException e) {
		}
		;
	}
}